package greentycoon.gui;

import greentycoon.batiment.centrale.CentraleCharbon;
import greentycoon.main.Main;
import greentycoon.main.VariablesGlobales;
import greentycoon.ville.Environement;
import greentycoon.ville.Ville;

import java.awt.Color;
import java.awt.Container;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.io.File;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;



public class MenuCreation extends JFrame implements ArgentListener,EnvironmentListener,PollutionListener, QuartierListener{

	private static final long serialVersionUID = 5200350643190915604L;
	private static final int TAILLE_ICONE_BOUTTON = 35;
	private static final int TAILLE_BOUTTON = 50;
	public static Ville ville;
	public static boolean afficherBoutonConstructionQuartier = false;

	public MenuCreation(Ville ville) {

		//super("menu",false,false,false,false);
		setTitle("menu");
		setSize(155, 550);
		setLocation(1000, 50);
		setVisible(true);
		setResizable(false);
		MenuCreation.ville=ville;
		setAlwaysOnTop(true);
	//	setUndecorated(true);
		construireMenu();
		setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
	}



	public void construireMenu(){

		Container contentPane = getContentPane();
		contentPane.setVisible(false);
		contentPane.removeAll();
		contentPane.setLayout(null);   


		/*Placement des bouttons*/

		/*Boutton centrale*/
		ImageIcon centraleCharbonIcon = rescaleImage(new File("src/greentycoon/gui/centraleCharbonIcon.png"), TAILLE_ICONE_BOUTTON, TAILLE_ICONE_BOUTTON);

		JButton centraleCharbonBoutton = new JButton(centraleCharbonIcon);
		centraleCharbonBoutton.setBounds(0, 0, TAILLE_BOUTTON, TAILLE_BOUTTON);
		centraleCharbonBoutton.setToolTipText("Centrales ");
		centraleCharbonBoutton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {

				VariablesGlobales.fenetreSelectionCentrale.setVisible(true);
				VariablesGlobales.fenetreSelectionDecharge.setVisible(false);
				VariablesGlobales.fenetreGestionTransport.setVisible(false);
				VariablesGlobales.fenetreSelectionCentreTri.setVisible(false);
				VariablesGlobales.choixBatiment = "";

			}
		});

		contentPane.add(centraleCharbonBoutton);

		/*Bouton decharge*/
		ImageIcon dechargeIcon = rescaleImage(new File("src/greentycoon/gui/dechargeIcon.png"), TAILLE_ICONE_BOUTTON, TAILLE_ICONE_BOUTTON);

		JButton dechargeBoutton = new JButton(dechargeIcon);
		dechargeBoutton.setBounds(TAILLE_BOUTTON, 0, TAILLE_BOUTTON, TAILLE_BOUTTON);
		dechargeBoutton.setToolTipText("Decharges");
		dechargeBoutton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {

				VariablesGlobales.fenetreSelectionDecharge.setVisible(true);
				VariablesGlobales.fenetreSelectionCentrale.setVisible(false);
				VariablesGlobales.fenetreGestionTransport.setVisible(false);
				VariablesGlobales.fenetreSelectionCentreTri.setVisible(false);
				VariablesGlobales.choixBatiment = "";

			}
		});
		
		contentPane.add(dechargeBoutton);
		
		/*Bouton Centre de Tri*/
		ImageIcon centreTriIcon = rescaleImage(new File("src/greentycoon/gui/centreTriIcon.png"), TAILLE_ICONE_BOUTTON, TAILLE_ICONE_BOUTTON);

		JButton centreTriBoutton = new JButton(centreTriIcon);
		centreTriBoutton.setBounds(TAILLE_BOUTTON*2, 0, TAILLE_BOUTTON, TAILLE_BOUTTON);
		centreTriBoutton.setToolTipText("Centres de tri");
		centreTriBoutton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {

				VariablesGlobales.fenetreSelectionCentreTri.setVisible(true);
				VariablesGlobales.fenetreSelectionDecharge.setVisible(false);
				VariablesGlobales.fenetreSelectionCentrale.setVisible(false);
				VariablesGlobales.fenetreGestionTransport.setVisible(false);
				VariablesGlobales.choixBatiment = "";

			}
		});
		
		
		
		contentPane.add(centreTriBoutton);
		
		
		/*Bouton Transport*/
		ImageIcon transportIcon = rescaleImage(new File("src/greentycoon/gui/transport.png"), TAILLE_ICONE_BOUTTON, TAILLE_ICONE_BOUTTON);

		JButton transportBoutton = new JButton(transportIcon);
		transportBoutton.setBounds(0, TAILLE_BOUTTON*4, TAILLE_BOUTTON, TAILLE_BOUTTON);
		transportBoutton.setToolTipText("Gestion des transports");
		transportBoutton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {

				VariablesGlobales.fenetreGestionTransport.setVisible(true);
				VariablesGlobales.fenetreSelectionCentrale.setVisible(false);
				VariablesGlobales.fenetreSelectionDecharge.setVisible(false);
				VariablesGlobales.fenetreSelectionCentreTri.setVisible(false);
				VariablesGlobales.choixBatiment = "";

			}
		});
		
		contentPane.add(transportBoutton);
		
		/*Bouton pour quitter*/
		ImageIcon quiterIcon = rescaleImage(new File("src/greentycoon/gui/fermer.png"), TAILLE_ICONE_BOUTTON, TAILLE_ICONE_BOUTTON);

		JButton quiterBoutton = new JButton(quiterIcon);
		quiterBoutton.setBounds(0, TAILLE_BOUTTON*6, TAILLE_BOUTTON, TAILLE_BOUTTON);
		quiterBoutton.setToolTipText("Quitter");
		quiterBoutton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				System.exit(0);

			}
		});
		
		contentPane.add(quiterBoutton);
		
		/*Bouton pour constuire un nouveau Quartier*/
		ImageIcon quartierIcon = rescaleImage(new File("src/greentycoon/gui/maison.png"), TAILLE_ICONE_BOUTTON, TAILLE_ICONE_BOUTTON);

		JButton quartierBoutton = new JButton(quartierIcon);
		quartierBoutton.setBounds(0, TAILLE_BOUTTON*5, TAILLE_BOUTTON, TAILLE_BOUTTON);
		quartierBoutton.setToolTipText("Nouveaux quartiers");
		if(afficherBoutonConstructionQuartier) {
			quartierBoutton.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {
					MenuCreation.ville.creerQuartier();
					MenuCreation.afficherBoutonConstructionQuartier = false;
				}
			});
		}
		else {
			quartierBoutton.setEnabled(false);
		}
		
		
		contentPane.add(quartierBoutton);
		
		/*Bouton pour quitter*/
		ImageIcon detruireIcon = rescaleImage(new File("src/greentycoon/gui/detruire.png"), TAILLE_ICONE_BOUTTON, TAILLE_ICONE_BOUTTON);

		JButton detruireBoutton = new JButton(detruireIcon);
		detruireBoutton.setBounds(TAILLE_BOUTTON, TAILLE_BOUTTON*6, TAILLE_BOUTTON, TAILLE_BOUTTON);
		detruireBoutton.setToolTipText("Detruire un batiment");
		detruireBoutton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				VariablesGlobales.choixBatiment="detruire";

			}
		});
		
		contentPane.add(detruireBoutton);
		
		/*Section information*/
		JLabel infoLabel = new JLabel("Informations");	    
		infoLabel.setBounds(33, 350, 150, 50);
		contentPane.add(infoLabel);

		/*Argent*/
		JLabel argentTexteLabel = new JLabel("Argent:");
		JLabel argentLabel = new JLabel(String.valueOf((VariablesGlobales.argent)));
		JLabel greenFlouzLabel = new JLabel(rescaleImage(new File("src/greentycoon/gui/greenFlouz.png"), 15, 15));
		argentTexteLabel.setBounds(1, 370, 50, 50);
		argentLabel.setBounds(50, 370, 50, 50);
		greenFlouzLabel.setBounds(100, 370, 50, 50);
		contentPane.add(argentTexteLabel);
		contentPane.add(argentLabel);
		contentPane.add(greenFlouzLabel);


		/*Pollution*/
		JLabel PollutionTextLabel = new JLabel("Pollution:");
		JLabel PollutionAirLabel = new JLabel("air: "+String.valueOf((int)ville.getPollution().getPollutionAir())) ;
		JLabel PollutionSolLabel = new JLabel("sol: "+String.valueOf((int)ville.getPollution().getPollutionSol())) ;
		PollutionTextLabel.setBounds(1, 390, 100, 50);
		PollutionAirLabel.setBounds(60, 390, 50, 50);
		PollutionSolLabel.setBounds(60, 410, 50, 50);
		contentPane.add(PollutionTextLabel);
		contentPane.add(PollutionAirLabel);
		contentPane.add(PollutionSolLabel);

		/*Meteo*/
		JLabel meteoLabel = new JLabel("Meteo:");
		JLabel soleilLabel = new JLabel("soleil: "+String.valueOf(Environement.getEnsoleillement())) ;
		JLabel ventLabel = new JLabel("vitesse vent: "+String.valueOf(Environement.getVitesseVent())) ;
		meteoLabel.setBounds(1, 430, 100, 50);
		soleilLabel.setBounds(60, 430, 50, 50);
		ventLabel.setBounds(50, 450, 100, 50);
		contentPane.add(meteoLabel);
		contentPane.add(soleilLabel);
		contentPane.add(ventLabel);



		contentPane.setVisible(true);
	}


	public ImageIcon rescaleImage(File source,int maxHeight, int maxWidth)
	{
		int newHeight = 0, newWidth = 0;        // Variables for the new height and width
		int priorHeight = 0, priorWidth = 0;
		BufferedImage image = null;
		ImageIcon sizeImage;

		try {
			image = ImageIO.read(source);        // get the image
		} catch (Exception e) {

			e.printStackTrace();
			System.out.println("Picture upload attempted & failed");
		}

		sizeImage = new ImageIcon(image);

		if(sizeImage != null)
		{
			priorHeight = sizeImage.getIconHeight(); 
			priorWidth = sizeImage.getIconWidth();
		}

		// Calculate the correct new height and width
		if((float)priorHeight/(float)priorWidth > (float)maxHeight/(float)maxWidth)
		{
			newHeight = maxHeight;
			newWidth = (int)(((float)priorWidth/(float)priorHeight)*(float)newHeight);
		}
		else 
		{
			newWidth = maxWidth;
			newHeight = (int)(((float)priorHeight/(float)priorWidth)*(float)newWidth);
		}


		// Resize the image

		// 1. Create a new Buffered Image and Graphic2D object
		BufferedImage resizedImg = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_ARGB_PRE);
		Graphics2D g2 = resizedImg.createGraphics();

		// 2. Use the Graphic object to draw a new image to the image in the buffer
		g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
		g2.drawImage(image, 0, 0, newWidth, newHeight, null);
		g2.dispose();

		// 3. Convert the buffered image into an ImageIcon for return
		return (new ImageIcon(resizedImg));
	}

	public static BufferedImage addImage(BufferedImage image1, BufferedImage image2){
		Graphics2D g2d = image1.createGraphics();
		g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
				RenderingHints.VALUE_ANTIALIAS_ON);
		g2d.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION,
				RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY);

		g2d.drawImage(image2, 0, 0, null);

		g2d.dispose();

		return image1 ;
	}

	public void mouseClicked( MouseEvent e )
	{
		// Clique gauche de la souris

		// Récupération de la position
		Point position = e.getPoint();
		System.out.println(position.getX()+" - "+position.getY());
		System.out.println("toto");

	}



	@Override
	public void actionArgentListener() {
		construireMenu();

	}



	@Override
	public void actionEnvironmentListener() {
		construireMenu();

	}



	@Override
	public void actionPollutionListener() {
		construireMenu();

	}



	@Override
	public void actionQuartierListener() {
		construireMenu();
		afficherBoutonConstructionQuartier = true;
		
	}


}
